/**
 * Date utility functions tests
 */

import '@testing-library/jest-dom';

// Mock date helpers
export const formatDate = (date: Date | string): string => {
  const d = new Date(date);
  return d.toLocaleDateString('zh-CN');
};

export const formatDateTime = (date: Date | string): string => {
  const d = new Date(date);
  return d.toLocaleString('zh-CN');
};

export const isToday = (date: Date | string): boolean => {
  const today = new Date();
  const targetDate = new Date(date);
  return (
    today.getDate() === targetDate.getDate() &&
    today.getMonth() === targetDate.getMonth() &&
    today.getFullYear() === targetDate.getFullYear()
  );
};

export const addDays = (date: Date | string, days: number): Date => {
  const d = new Date(date);
  d.setDate(d.getDate() + days);
  return d;
};

export const getRelativeTime = (date: Date | string): string => {
  const now = new Date();
  const targetDate = new Date(date);
  const diffInSeconds = Math.floor((now.getTime() - targetDate.getTime()) / 1000);

  if (diffInSeconds < 60) return '刚刚';
  if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)}分钟前`;
  if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)}小时前`;
  return `${Math.floor(diffInSeconds / 86400)}天前`;
};

describe('Date Utils', () => {
  describe('formatDate', () => {
    test('formats Date object correctly', () => {
      const date = new Date('2023-12-25');
      const result = formatDate(date);
      expect(result).toBe('2023/12/25');
    });

    test('formats date string correctly', () => {
      const result = formatDate('2023-12-25');
      expect(result).toBe('2023/12/25');
    });
  });

  describe('formatDateTime', () => {
    test('formats Date object with time correctly', () => {
      const date = new Date('2023-12-25T14:30:00');
      const result = formatDateTime(date);
      expect(result).toContain('2023/12/25');
      expect(result).toContain('14:30:00');
    });

    test('formats date string with time correctly', () => {
      const result = formatDateTime('2023-12-25T14:30:00');
      expect(result).toContain('2023/12/25');
      expect(result).toContain('14:30:00');
    });
  });

  describe('isToday', () => {
    test('returns true for today\'s date', () => {
      const today = new Date();
      const result = isToday(today);
      expect(result).toBe(true);
    });

    test('returns false for different date', () => {
      const yesterday = new Date();
      yesterday.setDate(yesterday.getDate() - 1);
      const result = isToday(yesterday);
      expect(result).toBe(false);
    });
  });

  describe('addDays', () => {
    test('adds positive days correctly', () => {
      const date = new Date('2023-12-25');
      const result = addDays(date, 5);
      expect(result.getDate()).toBe(30);
      expect(result.getMonth()).toBe(11); // December
      expect(result.getFullYear()).toBe(2023);
    });

    test('adds negative days correctly', () => {
      const date = new Date('2023-12-25');
      const result = addDays(date, -5);
      expect(result.getDate()).toBe(20);
      expect(result.getMonth()).toBe(11); // December
      expect(result.getFullYear()).toBe(2023);
    });

    test('handles month overflow correctly', () => {
      const date = new Date('2023-12-31');
      const result = addDays(date, 1);
      expect(result.getDate()).toBe(1);
      expect(result.getMonth()).toBe(0); // January
      expect(result.getFullYear()).toBe(2024);
    });
  });

  describe('getRelativeTime', () => {
    test('returns "刚刚" for recent time', () => {
      // Create a fixed "now" and test with relative time
      const now = new Date('2023-12-25T12:00:00');
      const date = new Date('2023-12-25T11:59:30');
      const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);

      // Test the logic directly
      const result = (() => {
        if (diffInSeconds < 60) return '刚刚';
        if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)}分钟前`;
        if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)}小时前`;
        return `${Math.floor(diffInSeconds / 86400)}天前`;
      })();

      expect(result).toBe('刚刚');
    });

    test('returns minutes for time within hour', () => {
      const now = new Date('2023-12-25T12:00:00');
      const date = new Date('2023-12-25T11:30:00');
      const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);

      const result = (() => {
        if (diffInSeconds < 60) return '刚刚';
        if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)}分钟前`;
        if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)}小时前`;
        return `${Math.floor(diffInSeconds / 86400)}天前`;
      })();

      expect(result).toBe('30分钟前');
    });

    test('returns hours for time within day', () => {
      const now = new Date('2023-12-25T12:00:00');
      const date = new Date('2023-12-25T08:00:00');
      const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);

      const result = (() => {
        if (diffInSeconds < 60) return '刚刚';
        if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)}分钟前`;
        if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)}小时前`;
        return `${Math.floor(diffInSeconds / 86400)}天前`;
      })();

      expect(result).toBe('4小时前');
    });

    test('returns days for older time', () => {
      const now = new Date('2023-12-25T12:00:00');
      const date = new Date('2023-12-23T12:00:00');
      const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);

      const result = (() => {
        if (diffInSeconds < 60) return '刚刚';
        if (diffInSeconds < 3600) return `${Math.floor(diffInSeconds / 60)}分钟前`;
        if (diffInSeconds < 86400) return `${Math.floor(diffInSeconds / 3600)}小时前`;
        return `${Math.floor(diffInSeconds / 86400)}天前`;
      })();

      expect(result).toBe('2天前');
    });

    test('works with real current time', () => {
      // Test that the actual function works with the current time
      const date = new Date();
      date.setMinutes(date.getMinutes() - 5); // 5 minutes ago
      const result = getRelativeTime(date);
      expect(result).toBe('5分钟前');
    });
  });
});